import store from '@/store'
import { parseTime } from '@/utils/functions'

/**
 * 通过对话索引查找对话列表下标
 *
 * @param {String} index_name
 */
export function findTalkIndex(friendName) {
  return store.state.talks.items.findIndex(
    item => item.friendName == friendName
  )
}

/**
 * 通过对话索引查找对话列表
 *
 * @param {String} index_name
 */
export function findTalk(friendName) {
  return store.state.talks.items.find(item => item.friendName == friendName)
}

/**
 * 格式化聊天对话列表数据
 *
 * @param {Object} params
 */
export function formateTalkItem(params) {
  let options = {
    id: 0,
    fromId: 0,
    fromName: '',
    fromProfile: '',
    toId: 0,
    toName: '',
    messageTypeId: 0,
    createTime: parseTime(new Date()),
    content: ''
  }

  Object.assign(options, params)

  options.index_name =
    options.type == 1
      ? `${options.type}_${options.friend_id}`
      : `${options.type}_${options.group_id}`

  return options
}
